package com.zxx.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.zxx.ssm.dao.Student;
import com.zxx.ssm.dao.StudentExample;
import com.zxx.ssm.model.biz.StudentBiz;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.StudentService;
import com.zxx.ssm.util.PaginationUtil;
import com.zxx.ssm.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("student")
@Controller
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("getStuList")
    @ResponseBody
    public BizResponse getStuList(@RequestParam String username, @RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "10") int pageSize,
                                  @RequestParam(defaultValue = "username") String sort, @RequestParam(defaultValue = "desc") String order) {
        PaginationUtil.preset(pageNumber, pageSize, sort, order);
        List<StudentBiz> studentBizs = studentService.getStuList(username);
        PageInfo pageInfo = new PageInfo(studentBizs);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("stuList", pageInfo);
        return bizResponse;
    }

    @RequestMapping("getAll")
    @ResponseBody
    public BizResponse getAll(@RequestParam String username, @RequestParam(defaultValue = "1") int pageNumber, @RequestParam(defaultValue = "10") int pageSize,
                                  @RequestParam(defaultValue = "username") String sort, @RequestParam(defaultValue = "desc") String order) {
        PaginationUtil.preset(pageNumber, pageSize, sort, order);
        StudentExample studentExample = new StudentExample();
        StudentExample.Criteria criteria = studentExample.createCriteria();
        if(!StringUtils.isBlank(username))
            criteria.andUsernameLike(username);
        List<Student> studentList = studentService.selectByExample(studentExample);
        PageInfo pageInfo = new PageInfo(studentList);
        BizResponse bizResponse = new BizResponse();
        bizResponse.put("stuList", pageInfo);
        return bizResponse;
    }

    @RequestMapping(value = "updateStudent", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse updateStudent(@ModelAttribute Student student){
        Student stu = studentService.selectByPrimaryKey(student.getId());
        stu.setGrescore(student.getGrescore());
        stu.setToeflscore(student.getToeflscore());
        stu.setIetsscore(student.getIetsscore());
        stu.setAgencyid(student.getAgencyid());
        studentService.updateByPrimaryKey(stu);
        return new BizResponse();
    }
}
